package com.epoint.androidphone.mobileoa.ui.login;

import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.epoint.androidmobile.bizlogic.dbservice.DBFrameUtil;
import com.epoint.androidmobile.bizlogic.dbservice.ConfigKey;
import com.epoint.androidmobile.bizlogic.login.task.LoginCheckTask;
import com.epoint.androidmobile.core.control.ToastUtil;
import com.epoint.androidmobile.core.phoneutil.PhoneHelp;
import com.epoint.androidmobile.core.phoneutil.Service;
import com.epoint.androidmobile.core.string.StringHelp;
import com.epoint.androidphone.R;
import com.epoint.androidphone.mobileoa.service.MainService;
import com.epoint.androidphone.mobileoa.ui.init.InitView;
import com.epoint.androidphone.mobileoa.ui.main.MainView;
import com.epoint.androidphone.mobileoa.ui.superview.SuperPhonePage;
import com.epoint.androidphone.mobileoa.ui.sysconfig.SysConfigView;
import com.epoint.androidmobile.bizlogic.update.task.UpdateTask;

/**
 * @author liyc
 * @time @2013-5-5 下午11:42:44
 * @annomation 登录
 */
public class LoginView extends SuperPhonePage{

	private Button btLogin;
	private long checkLoginTaskId,updateClientTaskId;
	private CheckBox cbRempsw,cbAutoLogin;
	private EditText etPassword;
	private AutoCompleteTextView etLoginId;
	private TextView tvVersion;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		dbUtil.setConfigValue(ConfigKey.isLogin, "0");
		HashMap<String, Object> params = getPassMap();
		params.put("updatepath", InitView.updatenPath);
		updateClientTaskId = new UpdateTask(this,params).startTask();
		
		addContentViewAndTitle(R.layout.loginview, "登录");
		tvVersion = (TextView) findViewById(R.id.tvVersion);
		getIvLeft().setVisibility(View.GONE);
		btLogin = (Button) findViewById(R.id.btLogin);
		btLogin.setOnClickListener(this);
		cbRempsw = (CheckBox) findViewById(R.id.cbRempsw);
		cbAutoLogin = (CheckBox) findViewById(R.id.cbAutoLogin);
		etLoginId = (AutoCompleteTextView) findViewById(R.id.etLoginId);
		etPassword = (EditText) findViewById(R.id.etPassword);
		getBtRight().setVisibility(View.VISIBLE);
		getBtRight().setText("设置");
		tvVersion.setText(PhoneHelp.getVersionName(this));
	}

	@Override
	protected void onResume() {
		super.onResume();

		//设置是否登陆状态为未登录
		dbUtil.setConfigValue(ConfigKey.isLogin, "0");

		etLoginId.setText(dbUtil.getConfigValue(ConfigKey.loginid));
		//是否记住密码
		String rempass = dbUtil.getConfigValue(ConfigKey.rempass);
		if(rempass.equals("1")){
			String upw = dbUtil.getConfigValue(ConfigKey.pw_un);

			etPassword.setText(upw);
			cbRempsw.setChecked(true);
		}

		//设置用户名下拉选项adapter
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
				R.layout.autocompleteview_list_item, dbUtil.getLoginHistoryInfo());
		etLoginId.setAdapter(adapter);

		//是否自动登陆
		String autologin = dbUtil.getConfigValue(ConfigKey.autologin);
		if(autologin.equals("1")){
			cbAutoLogin.setChecked(true);
			login();
		}
		else{
			cbAutoLogin.setChecked(false);
		}
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		if(v==btLogin)
		{
			login();
		}
		else if(v==getBtRight())
		{
			Service.intentView(this, SysConfigView.class);
		}
	}
	
	private void login()
	{
		if(etLoginId.getText().toString().trim().length()==0||etPassword.getText().toString().trim().length()==0)
		{
			ToastUtil.toastWorning(this, "用户名和密码不能为空！");
			return;
		}

		showDialogMiddle("正在验证身份...");
		HashMap<String, Object> params = getPassMap();
		params.put("loginid", etLoginId.getText().toString());
		params.put("password", etPassword.getText().toString());
		checkLoginTaskId = new LoginCheckTask(this,params).startTask();
	}


	@Override
	public void refreshUI(long taskId, Object obj) {
		super.refreshUI(taskId, obj);
		if(taskId==checkLoginTaskId)
		{
			if(validateXML(obj))
			{
				String bs = obj.toString();
				String UserArea = StringHelp.getXMLAttOut(bs, "UserArea");
				String UserLogin = StringHelp.getXMLAtt(UserArea, "UserLogin");
				if(UserLogin.equalsIgnoreCase("true"))
				{
					Intent it = new Intent(this,MainView.class);
					it.putExtra("NewMailsCount", StringHelp.getXMLAtt(UserArea, "NewMailsCount"));
					it.putExtra("NewWaitHandleCount", StringHelp.getXMLAtt(UserArea, "NewWaitHandleCount"));
					startActivity(it);
					
					finish();

					DBFrameUtil dbUtil = new DBFrameUtil(this);

					//更改是否登录字段
					dbUtil.setConfigValue(ConfigKey.isLogin, "1");
					//更新登陆历史记录表
					dbUtil.updateLoginHistory(etLoginId.getText().toString().trim());
					
					String userguid = StringHelp.getXMLAtt(bs, "UserGuid");
					dbUtil.setConfigValue(ConfigKey.userguid, userguid);
					String UserDisplayName = StringHelp.getXMLAtt(bs, "UserDisplayName");
					dbUtil.setConfigValue(ConfigKey.displayname, UserDisplayName);
					String BaseOuGuid = StringHelp.getXMLAtt(bs, "BaseOuGuid");
					dbUtil.setConfigValue(ConfigKey.BaseOuGuid, BaseOuGuid);
					dbUtil.setConfigValue(ConfigKey.loginid, etLoginId.getText().toString());
					if(cbRempsw.isChecked())
					{
						dbUtil.setConfigValue(ConfigKey.rempass, "1");
						dbUtil.setConfigValue(ConfigKey.pw_un, etPassword.getText().toString());
					}
					else
					{
						dbUtil.setConfigValue(ConfigKey.rempass, "0");
						dbUtil.setConfigValue(ConfigKey.pw_un, "");
					}

					if(cbAutoLogin.isChecked()){
						dbUtil.setConfigValue(ConfigKey.autologin, "1");
					}
					else{
						dbUtil.setConfigValue(ConfigKey.autologin, "0");
					}

				}
				else
				{
					ToastUtil.toastWorning(this, "用户名或密码错误!");
				}
			}
		}
		else if(taskId==updateClientTaskId)
		{
			System.out.println(obj.toString());
		}
	}
}
